$(function(){
	
	var getShopCart = function(){
        var $product = $.cookie("product_base");
        if($product != undefined){
            var json = strOper.get($product);
            var html = "";
            $.each(json,function(i,o){
            	html += "<h1 class=\"mod_main_cart title-s\">"
			+"<input type=\"checkbox\" id=\"warehouseall_12\" value=\"12\" checked=\"checked\">"
			+"<label for=\"warehouseall_12\">国内现货2号仓</label>"
		+"</h1>"
		+"<div class=\"mod_tb_cart\">"
			+"<table width=\"100%\" id=\"wh_12\">"
				+"<tbody>"
					+"<tr id=\"tr_7633473\">"
						+"<td width=\"5%\">"
							+"<input type=\"checkbox\" data=\"warehouse_12\" id=\"items_7633473\" value=\"7633473\" checked=\"checked\">"
						+"</td>"
						+"<td width=\"43%\">"
							+"<div class=\"goods clearfix\">"
								+"<a href=\"#\" class=\"goods_img\">"
									+"<img src=\""+o.img+"\">"
								+"</a>"
								+"<div class=\"goods_info\">"
									+"<a href=\"#\">"+o.content+"</a>"
									+"<p class=\"des\" id=\"sku_property_7633473\" style=\"display:block;\">规格：<span>默认</span></p>"
									+"<div id=\"act_7633473\">"
										+"<div class=\"cart-govm act\" style=\"cursor: default;\">"
											+"<span class=\"act_name\">特卖</span>"
										+"</div>"
									+"</div>"
								+"</div>"
							+"</div>"
						+"</td>"
						+"<td width=\"16%\">"
							+"<strong class=\"price\">¥34.00</strong>"
						+"</td>"
						+"<td width=\"12%\">"
							+"<div class=\"amount\">"
								+"<a id=\"decrement_7633473\" href=\"javascript:void(0);\" data-pid=\""+o.id+"\" class=\"btn_minus disabled\">-</a>"
								+"<input id=\"quantity_7633473\" class=\"buy_quantity\" type=\"text\" name=\"buy_quantity\" value=\""+o.count+"\">"
								+"<a id=\"increment_7633473\" href=\"javascript:void(0);\" data-pid=\""+o.id+"\" class=\"btn_plus\">+</a>"
							+"</div>"
						+"</td>"
						+"<td width=\"12%\">"
							+"<strong class=\"subtotal\">"+o.price*o.count+"</strong>"
						+"</td>"
						+"<td width=\"12%\">"
							+"<a id=\"remove_7633473\" href=\"javascript:void(0);\" data-pid=\""+o.id+"\" class=\"btn_del J_btn_del\" title=\"删除\"><img del=\"del\" data-pid=\""+o.id+"\" src=\"img/laji.png\"></a>"
						+"</td>"
					+"</tr>"
				+"</tbody>"
			+"</table>"
		+"</div>"
            });
            $(".w991").html(html);
		}
	}
		getShopCart();
		
		
 	var counter = function(element,type){
                var count = type === true ? 1 : -1;
              
                var $p = type === true ? $(element).prev() : $(element).next();
                if($p.val() == "1" && type === false){
                    count = 0;
                }
                else{
                    var $product = $.cookie("product_base");
                    var id = $(element).data("pid");
                    var result = strOper.counter($product,id,type);
                    $.cookie("product_base",result);
                }
                $p.val(parseInt($p.val()) + count);
            }
	$.get("goods1.json",function(data){
		var html = "";
		$.each(data,function(i,o){
			html += "<div class=\"ht_gallery\">"
						+"<div class=\"brand_name\">"
							+"<span class=\"flag\">"
							+"<img src=\"img/21_100x100.png\" width=\"36\" heigh=\"36\">"
							+"</span>"
							+"<h3 class=\"pro_name\">[特卖]"+o.content+"</h3>"
							+"<h4 class=\"pro_name_korea\"></h4>"
						+"</div>"
						+"<div class=\"ht_pic\">"
							+"<p class=\"brand_stockout\" style=\"display:none;\">抢光了!</p>"
							+"<a href=\"javascript:;\" class=\"lover\">"
								+"<span class=\"icon_lover\"></span><span>149</span>人收藏"
							+"</a>"
							+"<img id=\"bigPicture\" src=\""+o.img+"\">"
						+"</div>"
					+"</div>"
					+"<div class=\"product_information\">"
						+"<div class=\"cout_down\">"
							+"<i class=\"iconfont icon-shijian\"><img src=\"img/clock.png\"></i>距结束 "
					
							+"<span class=\"time\"><span>0天</span><strong>0时</strong><strong>0分</strong><strong>0秒</strong></span></span>"
						+"</div>"
						+"<p class=\"dec\">"+o.dec+"</p>"
						+"<div class=\"while_price termsparent clearfix\">"
							+"<em class=\"while_yen\">¥</em>"
							+"<b class=\"while_min\">"+o.num+"</b>"
							+"<p class=\"buy\" style=\"display:block;\"><span>205</span>人已购买</p>"
					
						+"</div>"
						+"<div class=\"details-zh\">官方指导价"
					
							+"<del class=\"while_max\">¥259.00</del>"
	
						+"</div>"
						+"<div class=\"details-jm\">"
							+"<span>包邮</span>"
							+"<div class=\"details-smt\">全场包邮</div>"
						+"</div>"
						+"<p class=\"place\">原装进口 国内现货</p>"
						+"<div class=\"ht_sku\">"
							+"<dl class=\"clearfix\">"
								+"<dt>颜色：</dt>"
								+"<dd optionindex=\"0\" optionid=\"5924\" class=\"active\"><a href=\"javascript:;\">默认</a></dd>"
							+"</dl>"
						+"</div>"
						+"<div class=\"ht_amount clearfix\">"
							+"<dt>数　　量：</dt>"
							+"<dd>"
								+"<div class=\"amount\">"
									+"<a href=\"javascript:void(0);\" pid=\""+o.pid+"\"  class=\"btn_minus astrict\">-</a>"
									+"<input type=\"text\" name=\"buyNum\" id=\"buyNum\" value=\"1\">"
									+"<a href=\"javascript:void(0);\" pid=\""+o.pid+"\"  class=\"btn_plus\">+</a>"
								+"</div>"
							+"</dd>"
						+"</div>"
						+"<div class=\"ht_action clearfix\">"
							+"<a id=\"buy_goods_btn\" href=\"javascript:;\"  class=\"btn btn_black50\" title=\"立即购买\" style=\"display:inline;\">"
								+"<span>立即购买</span>"
							+"</a>"
							+"<a id=\"add_cart_btn\" href=\"javascript:;\" class=\"btn btn_red50\" title=\"加入购物袋\" style=\"display: inline;\">"
								+"<span pid=\""+o.pid+"\"><i class=\"iconfont icon-gouwudai\"></i>加入购物袋</span>"
							+"</a>"
						+"</div>"
					+"</div>"
		});
		$(".product_base").html(html);
		
		$(".ht_action>a:eq(1)").click(function(event){
			 //ID#产品名#图片#价格#数量
            var $target = $(event.target);
            var id =$target.attr("pid");
            var content = $(".pro_name").html();
            var img = $("#bigPicture").attr("src");
            var price = $(".while_min").html();
            var str = id + "#" + img + "#" + content + "#" + price + "#" + "1";
            var $product = $.cookie("product_base");
            if(!$product){
                //cookie文件中是空的
                $.cookie("product_base",str,{
                    expires:7
                });
            }
            else{
            	$("#user_buy_num").html();
                var result = strOper.add($product,str);
                $.cookie("product_base",result);
            }
			  
			var offset = $('.user_top>li:eq(1)>.user_buy_num').offset(), flyer = $('<img class="u-flyer" src="img/lanrenzhijia.jpg"/>');
			flyer.fly({
			    start: {
			        left: event.pageX,
			        top: event.pageY - $(window).scrollTop()
			    },
			    end: {
			        left: offset.left,
			        top: offset.top - $(window).scrollTop(),
			        width: 20,
			        height: 20
			    }
			});
		});
		
			  $(".w991").click(function(e){
			  	//console.log($(e.target))
                if($(e.target).attr("del") == "del"){
                    //如果条件满足就代表点击的是删除的A标签
                    var $product = $.cookie("product_base");
                    var id = $(e.target).data("pid");
                    var result = strOper.del($product,id);
					//var xiaoji = $(".subtotal").html();
					
                    $.cookie("product_base",result);
                    
                    //对cookie更新后再次读取cookie并放置页面上
                    getShopCart();
                }
                else if($(e.target).attr("class") == "btn_minus disabled"){
                    //减少数量的事件 
                    //console.log(e.target)
                    counter(e.target,false);
       
                }
                else if($(e.target).attr("class") == "btn_plus"){
                    //增加数量的事件
                    
            		//console.log(id);
                    counter(e.target,true);
                }
            });
            
            $(".amount").click(function(e){
            	if($(e.target).attr("class") == "btn_minus astrict"){
                    //减少数量的事件 
                    //console.log(e.target)
                    counter(e.target,false);
       
                }
                else if($(e.target).attr("class") == "btn_plus"){
                    //增加数量的事件
                    counter(e.target,true);
                }
            });
            var total = $(".subtotal").html();
            $(".total_price").html(total);
	});
});
